# -*- coding: utf-8 -*-
# A.TNG

from google.appengine.ext import db

class XNUserInfo(db.Model):
    xnid = db.StringProperty(required=True) # string, xiaonei用户名
  
class RestaurantInfo(db.Model):
    #city = db.StringProperty() # string, 饭店所在城市
    name = db.StringProperty(required=True) # string, 饭店名称
    location = db.StringProperty(required=True) # string, 饭店具体地址
    tel = db.StringProperty(required=True) # string, 饭店联系电话
    type = db.StringProperty(required=True) # string, 饭店的类型：餐饮...
    jing = db.StringProperty(required=True) # string, 饭店的经度
    wei = db.StringProperty(required=True) # string, 饭店的纬度
    
class Comments(db.Model):
    average_consumption = db.IntegerProperty() # int, 平均消费
    service_attitude = db.IntegerProperty() # int, 服务态度，1-5，用星来表示
    content = db.TextProperty(required=True) # text, 用户评论
    commit_time = db.DateTimeProperty(auto_now=True) # datetime, 用户提交时间/用户更新评论时间
    ref_user = db.ReferenceProperty(XNUserInfo, required=True) # reference， 提交评论的用户，reference to XNUserInfo表
    ref_restaurant = db.ReferenceProperty(RestaurantInfo, required=True) # reference, 评论的饭店，reference to RestaurantInfo表

# XNUserInfo Datastore Operations

def get_user(xnid):
    """description: 取得用户表中的用户
    return:
        None/XNUserInfo 用户的实例
    """
    query = XNUserInfo.gql("WHERE xnid=:1", xnid)
    results = query.fetch(1)
    if len(results) == 0: 
        return None
    else:
        return results[0]

def create_user(xnid):
    """description: 创建用户
    return:
        None/key value 创建的新用户的key
    """
    u = get_user(xnid)
    if u is None:
        u = XNUserInfo(xnid='12345678')
        u.put()
    return u.key()

def update_user(key_id, xnid):
    """description: 更新用户资料
    return:
        key value 更新用户的用户id
    """
    u = XNUserInfo.get_by_id(key_id)
    if u is not None:
        u.xnid = xnid
        return u.put()
    else:
        return None

def delete_user(key_id):
    """description: 删除用户
    return:
        None
    """
    u = XNUserInfo.get_by_id(key_id)
    if u is not None:
        u.delete()

def get_restaurant(restaurant_name, restaurant_jing, restaurant_wei):
    """description: 取得饭店表中饭店的实例
    return:
        None/instance 对应饭店
    """    
    query = RestaurantInfo.gql("WHERE name=:1 AND jing=:2 AND wei=:3", 
                               restaurant_name, restaurant_jing, restaurant_wei)
    results = query.fetch(1)
    if len(results) == 0:
        return None
    else:
        return results[0]
    
def create_restaurant(rname, rlocation, rtel, rtype, rjing, rwei):
    """description: 创建饭店信息
    return:
        None/key value 创建的新的饭店的key
    """
    r = get_restaurant(rname, rjing, rwei)
    if r is None:
        r = RestaurantInfo(name=rname, 
                           location=rlocation,
                           tel=rtel,
                           type=rtype,
                           jing=rjing,
                           wei=rwei)
        r.put()
    return r.key()

def update_restaurant(key_id, rname, rlocation, rtel, rtype, rjing, rwei):
    """description: 更新饭店信息
    return:
        key value 更新的饭店的key
    """
    r = RestaurantInfo.get_by_id(key_id)
    if r is not None:
        r.name = rname
        r.location = rlocation
        r.tel = rtel
        r.type = rtype
        r.jing = rjing
        r.wei = rwei
        return r.put()
    else:
        return None

def delete_restaurant(key_id):
    r = RestaurantInfo.get_by_id(key_id)
    if r is not None:
        r.delete()

def create_comment(caverage_consumption, cservice_attitude, ccontent, cref_user, cref_restaurant):
    """description: 提交饭店的评论
    return:
        True/False 成功或者失败
    """
    c = Comments(average_consumption=caverage_consumption,
                 service_attitude=cservice_attitude,
                 content=ccontent,
                 ref_user=cref_user,
                 ref_restaurant=cref_restaurant)
    return c.put()

def get_user_comments(user_key_id):
    """description: 获取某用户提交的所有comments
    parsm:
        user_id: 用户表中代表用户的key_id
    return:
        用户提交的所有comments
    """
    u = XNUserInfo.get_by_id(user_key_id)
    if u is not None:
        return u.comments_set
    else:
        return None

def update_comment(key_id, average_consumption, service_attitude, content, ref_user, ref_restaurent):
    """description: 更新用户的评论
    return:
        True/False
    """
    c = Comments.get_by_id(key_id)
    if c is not None:
        c.average_consumption = average_consumption
        c.service_attitude = service_attitude
        c.content = content
        c.ref_user = ref_user
        c.ref_restaurent = ref_restaurent
        return c.put()
    else:
        return None

def delete_comment(key_id):
    """description: 删除用户的评论
    return: 
        True/False
    """
    c = Comments.get_by_id(key_id)
    if c is not None:
        c.delete()

